<template>
    <div class="pageContainer">
        <div class="treeTable">
            <div style="flex:1;">
                <el-form :inline="true" :model="mm" :rules="rules" ref="inForm">
                    <CText form v-model="mm.code" title="编码" field="code"></CText>
                    <CBtn @click="doSearchStock" style="margin-top:15px">查询</CBtn>
                    <CText :disabled="true" form v-model="mm.name" title="名称" field="name"></CText>
                    <CSelect :disabled="true" form v-model="mm.fkSecondTypeId" title="商品分类" field="fkSecondTypeId"
                        opType="opSecondGoodsType"></CSelect>
                    <CSelect :disabled="true" form v-model="mm.flowType" title="类型" field="flowType"
                        opType="opGoodsFlowType"></CSelect>
                    <CSelect :disabled="true" form v-model="mm.stockType" title="库存类型" field="stockType"
                        opType="opStockType"></CSelect>
                    <CText :disabled="true" form v-model="mm.curPrice" title="现价" field="curPrice"></CText>
                    <CTextarea :disabled="true" form v-model="mm.stetch" title="简述" field="stetch"></CTextarea>
                    <CSUpload v-model="mm.imgList" title="列表图" :readonly="true"></CSUpload>
                    <CText :disabled="true" form v-model="mm.name" title="名称" field="name"></CText>
                    <CTextNum v-if="mm.stockType == 1" form v-model="mm.stockNum" title="库存数量" field="stockNum"></CTextNum>
                    <div v-else-if="mm.stockType == 2" style="width: 80%;margin: 0px auto;">
                        <el-table :data="listSku" height="240" size="mini">
                            <el-table-column label="属性配对" prop="name"></el-table-column>
                            <el-table-column label="价格">
                                <template slot-scope="scope">
                                    <CText :disabled="true" v-model="scope.row.price" field="curPrice"></CText>
                                </template>
                            </el-table-column>
                            <el-table-column label="库存">
                                <template slot-scope="scope">
                                    <CTextNum v-model="scope.row.stock" field="stock"></CTextNum>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>


                    <div style="width:100%;text-align:center">
                        <el-button type="primary" @click="saveIn">确 定</el-button>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import pagingMixin from '@/mixin/pagingMixin';


export default {
    mixins: [pagingMixin],
    data() {
        return {
            mm: {},
            listSku: [],
            isStart: false
        }
    },
    created() {
    },
    mounted() {
        this.startScannerHandler();
    },
    methods: {
        startScannerHandler() {
            if (!this.isStart) {
                this._scanner = this.$scanner({
                    callback: v => {
                        this.mm.code = v;
                        console.log(111, this.mm.code)
                        this.doSearchStock();
                    }
                });
            } else {
                this._scanner.cancel();
            }
            this.isStart = !this.isStart;
        },
        saveIn() {
            this.$scx.saveInOut({
                product: this.mm,
                listSku: this.listSku
            }).then(res => {
                if (res.code == 200) {
                    this.$message.success("操作成功");
                }
            })
        },
        doSearchStock() {
            console.log(222, this.mm.code)
            this.$scx.getProductByCode({ code: this.mm.code }).then(res => {
                if (res.code == 200) {
                    this.mm = res.data.product;
                    this.listSku = res.data.listSku;
                } else {
                    this.$message.error("编码不存在");
                    this.mm = {};
                    this.listSku = [];
                }

            })
        }
    }
}
</script>

<style lang="scss" scoped>.roleWarp {
    display: flex;
    flex-wrap: wrap;
}</style>